//
//  StoreListViewController.m
//  BP_NewClient
//
//  Created by normal on 15-4-12.
//  Copyright (c) 2015年 normal. All rights reserved.
//

#import "StoreListViewController.h"
#import "UIImageView+AFNetworking.h"
#import "BP_NewClient-Swift.h"
#import "ProductViewController.h"
#import "StoreTableViewCell.h"
#import "StoreModel.h"
#import "CityModel.h"

@interface StoreListViewController (){
    UINib *nib;
    DropMenuView *dropMenu;
}
@property (nonatomic, strong) CityListModel *zoneListModel;

@property (nonatomic, strong) StroeListModel *storeListModel;
@end

@implementation StoreListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {

    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[AppDelegate shareApplication] hidesTabBar:YES animated:YES];
}

- (void)zoneRequest{
    
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    NSDictionary *cityModel = [standard objectForKey:@"reginInfo"];
    
    NSDictionary *dic = @{@"region_id":[cityModel objectForKey:@"region_id"]};
    
    [[BURequestManager sharedRequest] requestPostWithParameterWithBlockDic:CITY_DETAIL SetParamer:dic success:^(NSDictionary *responseDictionary){
        self.zoneListModel = [[CityListModel alloc] initWithDictionary:responseDictionary error:nil];
        
    } failure:^(NSString *errorMsg){

    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"商家列表";
    
    self.navigationItem.leftBarButtonItem = BACK_BAR_BUTTON_BACK( @selector(back:));
    self.view.backgroundColor = [UIColor whiteColor];
    myTableView.frame = CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height);

    dropMenu = [[DropMenuView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45) WithData:nil  DropMenuBlock:^(NSString *filterName,BOOL isShow){
    
        if (isShow) {
            dropMenu.frame = CGRectMake(0, 0, self.view.frame.size.width, 45);
        }else{
            dropMenu.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
        }
        [self.view bringSubviewToFront:dropMenu];
    }];
    
    [self.view addSubview:dropMenu];

    [self requestStoreList];
}

// 请求数据
- (void)requestStoreList{
    
    NSDictionary *dic = @{@"category":@"1",@"sort":@"2",@"latitude":@"53",@"longitude":@"73"};
    
    [[BURequestManager sharedRequest] requestPostWithParameterWithBlockDic:STORE_LIST SetParamer:dic success:^(NSDictionary *dic){
        
        self.storeListModel = [[StroeListModel alloc] initWithDictionary:dic error:nil];
        [myTableView reloadData];
 
    } failure:^(NSString *errMsg){
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.storeListModel.data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 116;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identy = @"StoreTableViewCell";
    StoreTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identy];
    NSArray *nibs;
    
    if (nil == cell) {
        nibs = [[NSBundle mainBundle]loadNibNamed:@"StoreTableViewCell" owner:nil options:nil];
        cell = [nibs lastObject];
    }
    
    cell.distance.textColor = [BPCommon colorWithHexValue:0xfa962a alpha:1.0f];
    
     StoreModel *model = (StoreModel *)self.storeListModel.data[indexPath.row];
    
    if (model.name) {
        cell.title.text = model.name;
    }else{
        cell.title.text = @"店名店名店名";
    }
    
    if (model.info) {
        cell.info.text = model.info;
    }
    
    [cell.imageIcon setImageWithURL1:[NSURL URLWithString:model.storeImg] placeholderImage:[UIImage imageNamed:@"storePic"]];
    cell.saleNum.text= [NSString stringWithFormat:@"%.1f km",([model.distance floatValue]/1000)];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    StoreModel *model = (StoreModel *)self.storeListModel.data[indexPath.row];

    
    ProductViewController *category = [[ProductViewController alloc] initWithNibName:@"ProductViewController" bundle:nil];
    category.model = model;
    [self.navigationController pushViewController:category animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
